package bhbt.service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.model.WageCurrent;
import bhbt.model.WageDetail;
import bhbt.model.WageHistory;
import bhbt.model.log.LogKuang;
import bhbt.service.core.BaseService;
import bhbt.service.log.LogKuangService;
import bhbt.service.log.LogStoreService;
import bhbt.utils.Param;






@Service
public class WageCurrentService extends BaseService {

	@Resource
	private MemberService memberService;
	 
	@Resource
	private ParamWageService paramWageService;
	 
	@Resource
	private WageDetailService wageDetailService;
	 
	@Resource
	private WageHistoryService wageHistoryService;
	 
	@Resource
	private LogStoreService logStoreService;
	@Resource
	private LogKuangService logKuangService;
	@Resource
	private DailiService dailiService;
	
	
	public void saveKuangCount() {
		//可购买数修改
		
		ParamWage paramWage800 = this.paramWageService.getByKey(800);
		
		Map<String, Object> mapParamWage = new HashMap<String, Object>();
		mapParamWage.put("id", 801);
		mapParamWage.put("value", paramWage800.getValue());
		this.paramWageService.update(mapParamWage);
		
		ParamWage paramWage1002 = this.paramWageService.getByKey(1002);	//每日产矿数
		
		//每日产矿石
		Map<String, Object> mapMember = new HashMap<String, Object>();
		mapMember.put("where", "1=1 and accountActive = 'T' and  activeKuang > 0 ");
		List<Member> listMember = this.memberService.queryForList("select", mapMember);
		
		 
		for(Member member : listMember){
			Map<String, Object> mapKuang = new HashMap<String, Object>();
	    	mapKuang.put("diyUpdate", "count(*) "); 
			mapKuang.put("where", " isOut='F' and isActive ='T' and yuDay > 0 and memberId = "+1000);
			Double kuangNum  = this.logKuangService.queryForObject("sumSql", mapKuang);//运行的矿点
			
			 
			//产矿数
			Double wageKuang = kuangNum*paramWage1002.getValue();
			
			//自己增加原石
			this.memberService.saveCountWage2(member, wageKuang, kuangNum,paramWage1002.getValue());
			
			//共享产值
			 
			Integer reId=member.getRecommendId();
			Member re = memberService.getByKey(reId);
			
			Integer cengRe=1;
			  
			while(re!=null){	
				
				Daili daili = this.dailiService.getByKey(re.getDailiId());
				if(re.getRecommendNum()>=cengRe&&cengRe<=daili.getBestGongCeng() ){
					
					Double wage3 = wageKuang * daili.getGongNum()*0.01;
					this.memberService.saveCountWage3(re,member,wage3, wageKuang);
					
				} 
				
				if(cengRe==10){
					break;
				}
				
				reId=re.getRecommendId();
				re = memberService.getByKey(reId);
				cengRe=cengRe+1;
				
			}
			
		}
		
		//矿石每日修改
		
		Map<String, Object> mapKuang = new HashMap<String, Object>();
	 
		mapKuang.put("where", " 1=1 and isOut='F' and isActive='T' and yuDay > 0 ");
		mapKuang.put("diyUpdate", " yuDay = yuDay - 1 ,totalNum =totalNum + "+paramWage1002.getValue());
		this.logKuangService.update(mapKuang);
		
		
		
		 
		//只剩一天，减少会员的运行矿数
		mapKuang = new HashMap<String, Object>();
		mapKuang.put("where", " 1=1 and isOut='F' and isActive='T' and yuDay <= 0 ");
		List<LogKuang> listKuang = this.logKuangService.queryForList("select", mapKuang);
		
		for(LogKuang logKuang : listKuang){
			
			mapMember = new HashMap<String, Object>();
			mapMember.put("id", logKuang.getMemberId());
			mapMember.put("diyUpdate", " activeKuang = activeKuang - 1");
			this.memberService.update(mapMember);
			
		}
		
		
		//更新矿点状态
		mapKuang = new HashMap<String, Object>();
		mapKuang.put("diyUpdate", " isOut = 'T' ");
		mapKuang.put("where", " 1=1 and isOut='F' and isActive='T' and yuDay <= 0 ");
		this.logKuangService.update(mapKuang);
		
	}
	
	/**
	 * @功能 计算工資
	 */
	public void saveWageCount() {
	  
		/*********************************** 工资汇总 ***********************************/
		Double allWage = 0d; // 总工資

		Double allWage1 = 0d; // 奖金1
		Double allWage2 = 0d; // 奖金2
		Double allWage3 = 0d; // 奖金3
		Double allWage4 = 0d; // 奖金4
		Double allWage5 = 0d; // 奖金5
		Double allWage6 = 0d; // 奖金6
		Double allWage7 = 0d; // 奖金7
		Double allWage8 = 0d; // 奖金8

		Integer allRecommendNum = 0; // 直推人数
		Integer allRecommendTotalNum = 0; // 团队直推人数
		Double allRecommendYe = 0d; // 直推业绩
		Double allRecommendTotalYe = 0d; // 直推业绩

		/********************* 持久化详细工资 *********************/
		WageDetail wageDetail = new WageDetail();
		Date date = new Date();
		long temp = date.getTime() - 24 * 60 * 60 * 1000;

		wageDetail.setCommitDate(new Date(temp));
		
		wageDetailService.saveOrUpdate(wageDetail);

		
		
		
		/********************* 循环计算工资 ************************/
		List<WageCurrent> wageList = this.queryForList("select",null);
		for (WageCurrent wage : wageList) {

			Integer memberId = wage.getMemberId();
			 
			Double wageNum = wage.getWageNum(); // 工资

			Double wage1 = wage.getWage1(); // 奖金1
			Double wage2 = wage.getWage2(); // 奖金2
			Double wage3 = wage.getWage3(); // 奖金3
			Double wage4 = wage.getWage4(); // 奖金4
			Double wage5 = wage.getWage5(); // 奖金5
			Double wage6 = wage.getWage6(); // 奖金6
			Double wage7 = wage.getWage7(); // 奖金7
			Double wage8 = wage.getWage8(); // 奖金7

			Integer recommendNum = wage.getRecommendNum(); // 直推人数
			Integer recommendTotalNum = wage.getRecommendTotalNum(); // 团队直推人数
			Double recommendYe = wage.getRecommendYe(); // 直推业绩
			Double recommendTotalYe = wage.getRecommendTotalYe();

			// 汇總
			allWage = allWage + wageNum; // 总工資
			allWage1 = allWage1 + wage1; // 奖金1
			allWage2 = allWage2 + wage2; // 奖金2
			allWage3 = allWage3 + wage3; // 奖金3
			allWage4 = allWage4 + wage4; // 奖金4
			allWage5 = allWage5 + wage5; // 奖金5
			allWage6 = allWage6 + wage6; // 奖金6
			allWage7 = allWage7 + wage7; // 奖金7
			allWage8 = allWage8 + wage8; // 奖金7

		
			allRecommendNum = allRecommendNum + recommendNum; // 直推人数
			allRecommendNum = allRecommendNum + recommendTotalNum; // 直推人数
			allRecommendYe = allRecommendYe + recommendYe; // 直推业绩
			allRecommendTotalYe = allRecommendTotalYe + recommendTotalYe; // 直推业绩

			
			// 生成累计工资
			WageHistory history = new WageHistory();

			history.setMemberId(memberId);
			history.setWageDetailId(wageDetail.getId());
			history.setWageNum(wageNum);

			history.setWage1(wage1);
			history.setWage2(wage2);
			history.setWage3(wage3);
			history.setWage4(wage4);
			history.setWage5(wage5);
			history.setWage6(wage6);
			history.setWage7(wage7);
			history.setWage8(wage8);

			history.setRecommendNum(recommendNum);
			history.setRecommendYe(recommendYe);
			history.setRecommendTotalNum(recommendTotalNum);
			history.setRemark("");

			history.setCommitDate(new Date(temp));

			wageHistoryService.saveOrUpdate(history);

		}
		
		// 清空當前工资
		
		Map<String, Object> mapWageCurrent = new HashMap<String, Object>();
		 
		mapWageCurrent.put("wageNum", 0);
		mapWageCurrent.put("wage1", 0);
		mapWageCurrent.put("wage2", 0);
		mapWageCurrent.put("wage3", 0);
		mapWageCurrent.put("wage4", 0);
		mapWageCurrent.put("wage5", 0);
		mapWageCurrent.put("wage6", 0);
		mapWageCurrent.put("wage7", 0);
		mapWageCurrent.put("wage8", 0);
		mapWageCurrent.put("daNum", 0);
		mapWageCurrent.put("shouNum", 0);
		mapWageCurrent.put("recommendNum", 0);
		mapWageCurrent.put("recommendTotalNum", 0);
		mapWageCurrent.put("recommendYe", 0);
		mapWageCurrent.put("recommendTotalYe", 0);
		
		this.update(mapWageCurrent);

		

		/********************* 持久化详细电子股 *********************/
		//优化后：（由请求6次数据库优化为只请求一次）
		ParamWage paramWage2000=new ParamWage();	// 当天激活人数
		ParamWage paramWage2001=new ParamWage();	// 当天激活业绩
		
		Map paraMap = new HashMap();
		paraMap.put("where", "1=1 and id in (2000,2001) ");
		List<ParamWage> paramWageList = this.paramWageService.queryForList("select", paraMap);
		for (ParamWage paramWage : paramWageList) {
			if(2000 == paramWage.getId()){
				paramWage2000 = paramWage;
				continue;
			}
			else if(2001 == paramWage.getId()){
				paramWage2001 = paramWage;
				continue;
			}
			 
			  
		}
		
	 
		double activeNumDouble = paramWage2000.getValue();	// 当天激活人数
		double activeYeDouble = paramWage2001.getValue();// 当天激活业绩
		
	  
		
		//清零: 当天激活人数、当天激活业绩、更新今日抢单数、出局人数
		Map<String,Object> mapParamWage=new HashMap<String, Object>();
		 
		mapParamWage.put("value", 0);
		mapParamWage.put("where", " id =2000 or id = 2001  ");
		paramWageService.update(mapParamWage);
		
		 
		
		Map<String,Object> mapWageDetail=new HashMap<String, Object>();
		mapWageDetail.put("id", wageDetail.getId());
		mapWageDetail.put("wageNum", allWage);
		mapWageDetail.put("wage1", allWage1);
		mapWageDetail.put("wage2", allWage2);
		mapWageDetail.put("wage3", allWage3);
		mapWageDetail.put("wage4", allWage4);
		mapWageDetail.put("wage5", allWage5);
		mapWageDetail.put("wage6", allWage6);
		mapWageDetail.put("wage7", allWage7);
		mapWageDetail.put("wage8", allWage8);
		
		mapWageDetail.put("recommendNum", allRecommendNum);
		mapWageDetail.put("recommendTotalNum", allRecommendTotalNum);
		mapWageDetail.put("recommendYe", allRecommendYe);
		mapWageDetail.put("recommendTotalYe", allRecommendTotalYe);
		mapWageDetail.put("activeNum", activeNumDouble);
		
		mapWageDetail.put("activeYe", activeYeDouble);

		wageDetailService.update(mapWageDetail);
		
		
		 

		Param.hui = true;
	}
	
	 

}
